package cn.edu.neusoft.yuejian4.yuejian_4.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.AboutActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.LoginActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.MainActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.MyCollectionActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.MyLikeClubActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.MySetActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.MyShowActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.OrderActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MyLikeClubAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.User;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.LoginModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;

/**
 * Created by Lix on 2018/10/9.
 */

public class MyFragment extends Fragment implements View.OnClickListener {

    private View view;
    private Button myorder,myshow,myselfset,myclear,myabout,mylogout;
    private TextView mynickname,mylike,mycollection;
    private ImageView myimg;
    private SharedPreferences sp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my,container,false);
        initViews();
        initEvent();
        sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        int uid = sp.getInt("uid",4);
        MyModel myModel = new MyModel();
        myModel.userinfo(uid,listener);
        return view;
    }

    TListener<User> listener = new TListener<User>() {
        @Override
        public void onResponse(User user) {
            mynickname.setText(user.getNickname());
            Picasso.with(getContext())
                    .load(Server.PIC_URL_J+user.getPic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(myimg);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initViews(){
        myorder = (Button)view.findViewById(R.id.my_order_btn);
        myshow = (Button)view.findViewById(R.id.my_show);
        myselfset = (Button)view.findViewById(R.id.my_myself_set_btn);
        myclear = (Button)view.findViewById(R.id.my_clear_btn);
        myabout = (Button)view.findViewById(R.id.my_about_btn);
        mylogout = (Button)view.findViewById(R.id.my_logout_btn);

        mynickname = (TextView) view.findViewById(R.id.my_nickname);
        mylike = (TextView) view.findViewById(R.id.my_like_btn);
        mycollection = (TextView) view.findViewById(R.id.my_collection_btn);

        myimg = (ImageView)view.findViewById(R.id.my_img);

    }
    private void initEvent(){
        myorder.setOnClickListener(this);
        myshow.setOnClickListener(this);
        myselfset.setOnClickListener(this);
        myclear.setOnClickListener(this);
        myabout.setOnClickListener(this);
        mylogout.setOnClickListener(this);

        mynickname.setOnClickListener(this);
        mylike.setOnClickListener(this);
        mycollection.setOnClickListener(this);
        myimg.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.my_logout_btn:
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.my_myself_set_btn:
                Intent intent1 = new Intent(getActivity(),MySetActivity.class);
                startActivity(intent1);
                break;
            case R.id.my_order_btn:
                Intent intent2 = new Intent(getActivity(),OrderActivity.class);
                startActivity(intent2);
                break;
            case R.id.my_like_btn:
                Intent intent3 = new Intent(getActivity(),MyLikeClubActivity.class);
                startActivity(intent3);
                break;
            case R.id.my_about_btn:
                Intent intent4 = new Intent(getActivity(),AboutActivity.class);
                startActivity(intent4);
                break;
            case R.id.my_show:
                Intent intent5 = new Intent(getActivity(),MyShowActivity.class);
                startActivity(intent5);
                break;
            case R.id.my_collection_btn:
                Intent intent6 = new Intent(getActivity(),MyCollectionActivity.class);
                startActivity(intent6);
                break;
        }

    }
}
